package com.fsi.client;

import com.fsi.client.util.MyDate;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PersonalGUI extends ServiceGUI {
	private static int imageCount = 0;
	private final VerticalPanel photoPanel;
	private final Image photo;

	private final Grid generalGrid;
	private final Label smokerLbl;
	private final Label descriptionLbl;
	private final Label firstDateLbl;

	private final Grid formGrid;
	private final Label userNameLbl;
	private final Label emailAddressLbl;
	private final Label birthdateLbl;
	private final Label genderLbl;
	private final Label countryLbl;
	private final Label postCodeLbl;

	public PersonalGUI() {
		photoPanel = new VerticalPanel();
		photo = new Image();
		photo.setHeight("150px");

		smokerLbl = new Label();
		descriptionLbl = new Label();
		firstDateLbl = new Label();

		userNameLbl = new Label();
		emailAddressLbl = new Label();
		birthdateLbl = new Label();
		genderLbl = new Label();
		countryLbl = new Label();
		postCodeLbl = new Label();

		generalGrid = new Grid(3, 2);
		formGrid = new Grid(8, 2);

		buildForm();

		this.add(photoPanel);
		this.add(new Label("Fixed Details"));
		this.add(formGrid);
		this.add(new Label("General Details"));
		this.add(generalGrid);
	}

	private void buildForm() {

		photoPanel.add(photo);

		generalGrid.setWidget(0, 0, new Label("Smoker?"));
		generalGrid.setWidget(0, 1, smokerLbl);

		generalGrid.setWidget(1, 0, new Label("Description"));
		generalGrid.setWidget(1, 1, descriptionLbl);

		generalGrid.setWidget(2, 0, new Label("First Date"));
		generalGrid.setWidget(2, 1, firstDateLbl);

		formGrid.setWidget(0, 0, new Label("User Name"));
		formGrid.setWidget(0, 1, userNameLbl);

		formGrid.setWidget(1, 0, new Label("Email Address"));
		formGrid.setWidget(1, 1, emailAddressLbl);

		formGrid.setWidget(2, 0, new Label("Birthdate"));
		formGrid.setWidget(2, 1, birthdateLbl);

		formGrid.setWidget(3, 0, new Label("Gender"));
		formGrid.setWidget(3, 1, genderLbl);

		formGrid.setWidget(4, 0, new Label("Country"));
		formGrid.setWidget(4, 1, countryLbl);

		formGrid.setWidget(5, 0, new Label("Postcode"));
		formGrid.setWidget(5, 1, postCodeLbl);

	}

	@SuppressWarnings("deprecation")
	void loadDetails(Customer customer) {
		try {
			photo.setUrl(GWT.getModuleBaseURL() + "getphoto?nick="
					+ customer.getPhotoID() + "&variable=" + imageCount++);

			this.smokerLbl.setText(customer.isSmoker() ? "Yes" : "No");
			this.descriptionLbl.setText(customer.getDescription());
			this.firstDateLbl.setText(customer.getFirstDate());

			this.userNameLbl.setText(customer.getUserName());
			this.emailAddressLbl.setText(customer.getEmailAddress());
			this.birthdateLbl
					.setText(Integer
							.toString(customer.getBirthdate().getDate())
							+ " "
							+ MyDate.months[customer.getBirthdate().getMonth()]
							+ ", "
							+ Integer.toString(1900 + customer.getBirthdate()
									.getYear()));
			this.genderLbl.setText(Customer.GENDERS[customer.getGender()]);
			this.countryLbl.setText(Customer.COUNTRIES[customer.getCountry()]);
			this.postCodeLbl.setText(customer.getPostCode());
		} catch (Exception e) {
			gui.setStatus("Error loading details: " + e.getMessage());
		}
	}
}
